﻿Imports System.Runtime.Serialization.Formatters.Binary
<Serializable()>
Public Class Group
    Implements ICloneable
    Private WithEvents pCars As New VMGenLibrary.VMObjects.PowerCollection(Of L3DObjectsLibrary.Car)
    Private NameValue As String
    Public Event NowEmpty(ByVal sender As L3DObjectsLibrary.Group)
    Public Event DateChanged(ByVal sender As Object, ByVal e As L3DObjectsLibrary.DateChangedEventArgs)
    Public Event QueryChange(ByVal sender As Object, ByVal e As QueryChangeEventArgs)
#Region "PROPERTIES"
    Public Property Name As String
        Get
            Return NameValue
        End Get
        Set(ByVal value As String)
            NameValue = value
        End Set
    End Property
    Public ReadOnly Property TotalGroupKmStatus(Optional ByVal EOBD As Boolean = False) As Integer
        Get
            Dim pTotalGroupKm As Integer
            If Me.Cars.Count > 0 Then
                For Each Car As L3DObjectsLibrary.Car In Me.Cars
                    If Car.EOBD = True And EOBD = True Or EOBD = False Then
                        pTotalGroupKm = pTotalGroupKm + Car.kmStatus
                    End If
                Next
            End If
            Return pTotalGroupKm
        End Get
    End Property
    Public ReadOnly Property AllCarsKm(Optional ByVal EOBDOnly As Boolean = False, Optional ByVal EOBDExclude As Boolean = False) As Integer
        Get
            Dim km As Integer
            If Me.Cars.Count > 0 Then
                For Each Car As L3DObjectsLibrary.Car In Me.Cars
                    If (Car.EOBD = True And EOBDOnly = True) Or (Car.EOBD = False And EOBDExclude = True) Or (EOBDOnly = False And EOBDExclude = False) Then
                        km = km + Car.kmCity + Car.kmHS + Car.kmRG
                    End If
                Next
            End If
            Return km
        End Get
    End Property
    Public ReadOnly Property GroupStartDate As Date
        Get
            Dim pResultDate As Date
            For Each pCar As L3DObjectsLibrary.Car In Me.Cars
                If DateTime.Compare(pCar.CarStartDate, pResultDate) < 0 Or pCar Is Me.Cars.First Then
                    pResultDate = pCar.CarStartDate
                End If
            Next
            Return pResultDate
        End Get
    End Property
    Public ReadOnly Property GroupEndDate As Date
        Get
            Dim pResultDate As Date
            For Each pCar As L3DObjectsLibrary.Car In Me.Cars
                If DateTime.Compare(pCar.CarEndDate, pResultDate) > 0 Or pCar Is Me.Cars.First Then
                    pResultDate = pCar.CarEndDate
                End If
            Next
            Return pResultDate
        End Get
    End Property
    Public ReadOnly Property HasEOBDCar As Boolean
        Get
            Dim Controllo As Boolean = False
            For Each Car As L3DObjectsLibrary.Car In Me.Cars
                If Car.EOBD = True Then
                    Controllo = True
                    Exit For
                End If
            Next
            Return Controllo
        End Get
    End Property
    Public ReadOnly Property GroupRealStartDate As Date
        Get
            Dim pResultDate As Date
            For Each pCar As L3DObjectsLibrary.Car In Me.Cars
                If DateTime.Compare(pCar.CarRealStartDate, pResultDate) < 0 Or pCar Is Cars.First Then
                    pResultDate = pCar.CarRealStartDate
                End If
            Next
            Return pResultDate
        End Get
    End Property
    Public ReadOnly Property GroupRealEndDate As Date
        Get
            Dim pResultDate As Date
            For Each pCar As L3DObjectsLibrary.Car In Me.Cars
                If DateTime.Compare(pCar.CarRealEndDate, pResultDate) > 0 Or pCar Is Cars.First Then
                    pResultDate = pCar.CarRealEndDate
                End If
            Next
            Return pResultDate
        End Get
    End Property
    Public Property Cars As VMGenLibrary.VMObjects.PowerCollection(Of L3DObjectsLibrary.Car)
        Get
            Return pCars
        End Get
        Set(ByVal value As PowerCollection(Of L3DObjectsLibrary.Car))
            pCars.Dispose()
            pCars = value
        End Set
    End Property
#End Region
#Region "METHODS"
    Public Function Clone() As Object Implements ICloneable.Clone
        Return ObjManagement.GeneralFunctions.CloneObject(Me)
    End Function
    Public Function MileageForecast(ByVal CurrentStatusDate As Date, ByVal ForecastStatusDate As Date, Optional ByVal EOBDOnly As Boolean = False, Optional ByVal EOBDExclude As Boolean = False) As Integer
        Dim KmForecast As Integer
        For Each Car As L3DObjectsLibrary.Car In Me.Cars
            If (EOBDOnly = True And Car.EOBD = True) Or (Car.EOBD = False And EOBDExclude = True) Or (EOBDOnly = False And EOBDExclude = False) Then
                KmForecast = KmForecast + Car.MileageForecast(CurrentStatusDate, ForecastStatusDate)
            End If
        Next
        Return KmForecast
    End Function
#End Region
#Region "EVENTS"
    Private Sub Cars_CountChanging(ByVal sender As VMGenLibrary.VMObjects.PowerCollection(Of L3DObjectsLibrary.Car), ByVal e As VMGenLibrary.PowercollectionEventArgs) Handles pCars.CountChanging, pCars.ItemsExchanging, pCars.ItemsMoving
        RaiseEvent QueryChange(Me, New QueryChangeEventArgs)
    End Sub
    Private Sub Cars_CountChanged(ByVal sender As VMGenLibrary.VMObjects.PowerCollection(Of L3DObjectsLibrary.Car), ByVal e As VMGenLibrary.PowercollectionEventArgs) Handles pCars.CountChanged
        Dim Car As L3DObjectsLibrary.Car = CType(e.ObjectMoved, L3DObjectsLibrary.Car)
        If e.Action = CountChangedAction.Added Then
            AddHandler Car.DateChanged, AddressOf Car_DateChanged
            AddHandler Car.QueryChange, AddressOf Car_QueryChange
        ElseIf e.Action = CountChangedAction.Removed Then
            RemoveHandler Car.DateChanged, AddressOf Car_DateChanged
            RemoveHandler Car.QueryChange, AddressOf Car_QueryChange
        End If
        RaiseEvent DateChanged(Me, New L3DObjectsLibrary.Structures.DateChangedEventArgs)
    End Sub
    Private Sub Cars_NowEmpty(ByVal sender As Object) Handles pCars.NowEmpty
        RaiseEvent NowEmpty(Me)
    End Sub
    Private Sub Car_DateChanged(ByVal sender As Object, ByVal e As L3DObjectsLibrary.DateChangedEventArgs)
        RaiseEvent DateChanged(Me, e)
    End Sub
    Private Sub Car_QueryChange(ByVal sender As Object, ByVal e As QueryChangeEventArgs)
        RaiseEvent QueryChange(Me, e)
    End Sub
#End Region
End Class


